package edu.cicese.alz;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 22/03/12
 * Time: 06:18 PM
 */
public class AccelerometerCount {

	public static float alpha = 0.8f;
	public static float alphaI = 0.2f;
	private static double[] gravity = new double[3];

	public static void setAlphaValue(float updateFreq, float cutOffFreq) {
		// alpha is calculated as t / (t + dT)
        // with t, the low-pass filter's time-constant
        // and dT, the event delivery rate

		float t = 1.0f / cutOffFreq;
		float dT = 1.0f / updateFreq;

		alpha = t / (t + dT);
		alphaI = 1 - alpha;
	}

	public static void setAlpha(float alpha) {
		AccelerometerCount.alpha = alpha;
		alphaI = 1 - alpha;
	}

	public static void initiateGravity() {
		gravity = new double[]{0, 0, 0};
	}

	public static double[] getFilteredAcceleration(double axisX, double axisY, double axisZ) {
		gravity[0] = (alpha * gravity[0]) + (alphaI * axisX);
		gravity[1] = (alpha * gravity[1]) + (alphaI * axisY);
		gravity[2] = (alpha * gravity[2]) + (alphaI * axisZ);

		return new double[]{axisX - gravity[0], axisY - gravity[1], axisZ - gravity[2]};
	}


	public static void getActivityCountsPerDay(String path) {

		File dir = new File(path);
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.toLowerCase().endsWith(".ct");
			}
		};
		File[] files = dir.listFiles(filter);

		if (files != null) {
			int day = 0;

			for (File file : files) {
				try {
					double[][] valuesAux = AccelerometerChart.loadData(file.getCanonicalPath());
					double count = 0;
					for (int i = 0; i < 1440; i++) {
						count += valuesAux[2][i];
					}
					day++;
					System.out.println("Day " + day + ": " + (count / 1440) + ".");

				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void getActivityCountsPerHour(String path) {

		File dir = new File(path);
		FilenameFilter filter = new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.toLowerCase().endsWith(".ct");
			}
		};
		File[] files = dir.listFiles(filter);

		if (files != null) {
			int day = 0;
			double[][] valuesAux;
			double count;
			int mins = 0;
			String countString;

			for (File file : files) {
				try {
					valuesAux = AccelerometerChart.loadData(file.getCanonicalPath());
					count = 0;
					countString = "";

					for (int i = 0; i < 1440; i++) {
						count += valuesAux[2][i];
						mins++;
						if (mins == 60) {
							countString += String.format("%.2f", (float)(count / 60)) + " ";
							count = 0;
							mins = 0;
						}
					}
					day++;
					System.out.println("Day " + day + ": " + countString + ".");

				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

//	public static double[] getFilteredAcceleration(double axisX, double axisY, double axisZ,
//	                                               double gAxisX, double gAxisY, double gAxisZ) {
//		gravity[0] = (alpha * gAxisX) + (alphaI * axisX);
//		gravity[1] = (alpha * gAxisY) + (alphaI * axisY);
//		gravity[2] = (alpha * gAxisZ) + (alphaI * axisZ);
//
//		return new double[]{axisX - gravity[0], axisY - gravity[2], axisZ - gravity[2]};
//	}
}
